package com.xinghou.platform.server.api.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xinghou.platform.util.StringUtils;
import com.xinghou.platform.util.exception.DBExceptionEnums;
import com.xinghou.platform.util.exception.ServiceException;
import com.xinghou.platform.server.api.log.ApiLogger;
import org.slf4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.WebUtils;

import com.google.gson.reflect.TypeToken;
import com.xinghou.platform.entity.req.ShopInfoReq;
import com.xinghou.platform.entity.resp.ShopInfoListResp;
import com.xinghou.platform.entity.resp.ShopInfoResp;
import com.xinghou.platform.entity.resp.ShopjobListResp;
import com.xinghou.platform.entity.resp.ShopjobResp;
import com.xinghou.platform.entity.resp.TastInfoListResp;
import com.xinghou.platform.entity.resp.TastInfoResp;
import com.xinghou.platform.server.api.bussiness.ShopInfoBusinessService;
import com.xinghou.platform.server.api.bussiness.ShopjobInfoBusinessService;
import com.xinghou.platform.server.api.bussiness.TastInfoBusinessService;
import com.xinghou.platform.server.api.log.LoggerFactory;
import com.xinghou.platform.util.ConfigUtil;
import com.xinghou.platform.util.JsonUtil;
import com.xinghou.platform.util.SignUtil;

@Controller
@SessionAttributes("token")
public class ShopInfoController extends BaseControl {
    private String key = ConfigUtil.getProperty("HTTP_SERVICE_KEY");
    private Logger logInfo = LoggerFactory.getServerInfoLogger(ShopInfoController.class);

    private String reqjson;//请求json
    private String repjson;//返回json

    @Resource
    private ShopInfoBusinessService shopInfoBusinessService;

    @Resource
    private ShopjobInfoBusinessService shopjobInfoBusinessService;

    @Resource
    private TastInfoBusinessService tastInfoBusinessService;

    private ShopInfoListResp shopInfoListResp;
    private ShopInfoResp shopInfoResp;
    private ShopjobResp shopjobResp;
    private ShopjobListResp shopjobListResp;
    private TastInfoResp tastInfoResp;
    private TastInfoListResp tastInfoListResp;





    /**
     * 查询门店信息模糊
     *
     * @param shopInfoReq
     * @param request
     * @param response
     * @return
     * @throws UnsupportedEncodingException
     * @throws DataAccessException
     */
    @ResponseBody
    @RequestMapping(value = "/searchShops", method = {RequestMethod.POST, RequestMethod.GET})
    public String findShop(
                           @RequestBody ShopInfoReq shopInfoReq,
                           HttpServletRequest request, HttpServletResponse response) throws DataAccessException, UnsupportedEncodingException {
        shopInfoListResp = new ShopInfoListResp();
        reqjson = JsonUtil.toJson(shopInfoReq);
        shopInfoReq.setDistance("1000");
        if(StringUtils.isEmpty(shopInfoReq.getLongitude())||StringUtils.isEmpty(shopInfoReq.getLatitude()))
            throw new ServiceException(DBExceptionEnums.LON_LAT_IS_NULL);
        List<ShopInfoResp> sirp = shopInfoBusinessService.getStoreSearchList(shopInfoReq);
        if(sirp==null||sirp.isEmpty()) sirp=new ArrayList<ShopInfoResp>();
        repjson = JsonUtil.toJson(sirp);


        return response(sirp);
    }

    /**
     * 查询门店信息详细
     *
     * @param shopInfoReq
     * @param request
     * @param response
     * @return
     * @throws UnsupportedEncodingException
     * @throws DataAccessException
     */
    @ResponseBody
    @RequestMapping(value = "/findShopDetail", method = {RequestMethod.POST, RequestMethod.GET})
    public String findShopDetail(
                                 @RequestBody ShopInfoReq shopInfoReq,
                                 HttpServletRequest request, HttpServletResponse response) throws DataAccessException, UnsupportedEncodingException {
        shopInfoListResp = new ShopInfoListResp();
        ShopInfoResp resp=shopInfoBusinessService.getShopInfoDetail(shopInfoReq.getUserid(),shopInfoReq.getShopid());
        repjson = JsonUtil.toJson(resp);
        return response(resp);
    }


    public ShopInfoListResp getShopInfoListResp() {
        return shopInfoListResp;
    }

    public void setShopInfoListResp(ShopInfoListResp shopInfoListResp) {
        this.shopInfoListResp = shopInfoListResp;
    }

    public ShopInfoResp getShopInfoResp() {
        return shopInfoResp;
    }

    public void setShopInfoResp(ShopInfoResp shopInfoResp) {
        this.shopInfoResp = shopInfoResp;
    }

    public ShopjobResp getShopjobResp() {
        return shopjobResp;
    }

    public void setShopjobResp(ShopjobResp shopjobResp) {
        this.shopjobResp = shopjobResp;
    }

    public ShopjobListResp getShopjobListResp() {
        return shopjobListResp;
    }

    public void setShopjobListResp(ShopjobListResp shopjobListResp) {
        this.shopjobListResp = shopjobListResp;
    }

    public TastInfoResp getTastInfoResp() {
        return tastInfoResp;
    }

    public void setTastInfoResp(TastInfoResp tastInfoResp) {
        this.tastInfoResp = tastInfoResp;
    }

    public TastInfoListResp getTastInfoListResp() {
        return tastInfoListResp;
    }

    public void setTastInfoListResp(TastInfoListResp tastInfoListResp) {
        this.tastInfoListResp = tastInfoListResp;
    }

    public String getReqjson() {
        return reqjson;
    }

    public void setReqjson(String reqjson) {
        this.reqjson = reqjson;
    }

    public String getRepjson() {
        return repjson;
    }

    public void setRepjson(String repjson) {
        this.repjson = repjson;
    }

}
